﻿package controllers.operset;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import rbac.models.Oplog;
import rbac.models.User;

import utils.PinyingUtil;

import models.Zjlist;

import com.et.mvc.BinaryView;
import com.et.mvc.JsonView;
import com.et.mvc.MultipartFile;
import com.et.mvc.TextView;
import com.et.mvc.View;

import com.et.mvc.filter.BeforeFilter;
import com.et.mvc.filter.BeforeFilters;

import controllers.ApplicationController;


public class ZjlistController extends ApplicationController {
	
	public void index() throws Exception{
    	
	}
	
	
	public View getZjlistItems(int page, int rows, String q,String t, String sort, String order) throws Exception {
		User user = (User) rbac.getCurrentUser();
		String orderBy = "id";
		if (sort != null && order != null){
			orderBy = sort + " " + order;
		}
		String cond = "1=1";
		List<Object> tmpArgs = new ArrayList<Object>();
		
		/**
		if (t != 0){
			cond += " and (parentId = ?)";
			tmpArgs.add(t);
		}
		
		if (q != null){
			cond += " and (name like '%"+q+"%' or memo like '%"+q+"%' ) ";			
		}
		**/
		if (t != null){
			cond += " and (mainid = '"+t+"' or company_id = '"+t+"')";
		}
		
		 if (q != null) {
		      cond = cond + " and (mc like '%" + q + "%'  or xm like '%" + q + "%' or dz1 like '%" + q + "%' ) ";
		    }

		 cond += " and id = " + user.getDepartmentid();
		 
		Object[] args = tmpArgs.toArray();
		
		long total = Zjlist.count(Zjlist.class, cond, args);
		List<Zjlist> items = Zjlist.findAll(Zjlist.class, cond, args, orderBy, rows, (page-1)*rows);
		
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("total", total);
		result.put("rows", items);
		
		return new JsonView(result);
	}
	
	
	
	public View updateZjlist(int id, Map<String,Object> params, String[] rids) throws Exception {
		Map<String,Object> result = new HashMap<String,Object>();
		Zjlist item = Zjlist.find(Zjlist.class, id);
		JsonView olditem= new JsonView(item);
		item = Zjlist.updateModel(item, params);
    //其他需要系统自动填写的字段
    //其他操作如写入日志
		if (item.save()){
			User user = (User) rbac.getCurrentUser();				
			Oplog.addMsg(user.getLogin(), user.getName() + "("+getIpAddr(request)+")修改单位资料，原内容为："+ olditem + "\r\n修改内容为：" + new JsonView(item));										
			
			result.put("success", true);
		} else {
			result.put("failure", true);
			result.put("msg", getErrorMsg(item));
		}
		
		View view = new JsonView(result);
		view.setContentType("text/plain;charset=utf-8");
		return view;
	}
	
	
	
	
}
